package com.bmf.module.rbac.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.module.rbac.entity.Organization;
import com.bmf.module.rbac.entity.RbacPerms;
import com.bmf.module.rbac.mapper.RbacPermsMapper;
import com.bmf.module.rbac.service.RbacPermsService;
import com.bmf.module.rbac.vo.OrganizationResultVo;
import com.bmf.module.rbac.vo.RbacPermsCreateVo;
import com.bmf.module.rbac.vo.RbacPermsResultVo;
import com.bmf.module.rbac.vo.RbacPermsUpdateVo;
import com.bmf.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (RbacPerms)表服务实现类
 *
 * @author makejava
 * @since 2022-12-27 15:33:18
 */
@Service("rbacPermsService")
public class RbacPermsServiceImpl extends ServiceImpl<RbacPermsMapper, RbacPerms> implements RbacPermsService {

    @Override
    public List<RbacPermsResultVo> selectAll() {
        List<RbacPerms> list = list();
        if(CollUtil.isNotEmpty(list)){
            List<RbacPermsResultVo> rbacPermsResultVos = BeanCopyUtils.copyBeanList(list, RbacPermsResultVo.class);
            return rbacPermsResultVos.stream().filter(s -> s.getParentid() == 0).peek(s -> setChildren(rbacPermsResultVos, s)).collect(Collectors.toList());
        }
        return null;
    }
    private void setChildren(List<RbacPermsResultVo> rbacPermsResultVos,RbacPermsResultVo rbacPermsResultVo){
        List<RbacPermsResultVo> collect = rbacPermsResultVos.stream().filter(s -> s.getParentid().equals(rbacPermsResultVo.getId())).peek(s -> setChildren(rbacPermsResultVos, s))
                .collect(Collectors.toList());
        rbacPermsResultVo.setChildrens(collect);
    }

    @Override
    public RbacPermsResultVo selectOne(Serializable id) {
        RbacPerms byId = getById(id);
        if(ObjectUtil.isEmpty(byId)){
            return null;
        }
        return BeanCopyUtils.copyBean(byId, RbacPermsResultVo.class);
    }

    @Override
    public Integer insert(RbacPermsCreateVo rbacPermsCreateVo) {
        RbacPerms rbacPerms = BeanCopyUtils.copyBean(rbacPermsCreateVo, RbacPerms.class);
        save(rbacPerms);
        return rbacPerms.getId();
    }

    @Override
    public void update(RbacPermsUpdateVo rbacPermsUpdateVo) {
        RbacPerms rbacPerms = BeanCopyUtils.copyBean(rbacPermsUpdateVo, RbacPerms.class);
        updateById(rbacPerms);
    }

    @Override
    public void delete(List<Integer> ids) {
        removeByIds(ids);
    }
}
